package com.alili_tv.system.controller;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.common.R;
import com.alili_tv.system.entity.Follow;
import com.alili_tv.system.entity.User;
import com.alili_tv.system.entity.Video;
import com.alili_tv.system.service.FollowService;
import com.alili_tv.system.service.UserService;
import com.alili_tv.system.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/search", produces = "application/json")
@Slf4j
public class SearchController {

    @Resource
    private UserService userService;

    @Resource
    private VideoService videoService;

    @Resource
    private FollowService followService;

    /**
     * @param content
     * @param type    1：视频 2：用户
     * @param sort    1：最多点击 2：最新发布
     * @param time    1：全部时长 2：10分钟以下 3：10-30分钟 4：30-60分钟 5：60分钟以上
     * @param channel 1：全部分区 else：分区id
     */
    @GetMapping
    public R<?> search(String content,
                       Integer type,
                       Integer sort,
                       Integer time,
                       Integer channel) {
        if (type == 1) {
            // 查询出视频信息
            List<Video> videos = videoService.searchVideos(content, sort, time, channel);
            Set<Long> userIds = videos.stream().map(Video::getCreateUser).collect(Collectors.toSet());
            Map<Long, User> userMap = userService.getByIds(userIds);
            videos.forEach(video -> {
                User user = userMap.get(video.getCreateUser());
                video.setUser(user);
            });
            return R.success(videos);
        } else {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                    .like(User::getName, content)
                    .orderByAsc(User::getCreateTime);
            List<User> users = userService.list(queryWrapper);
            Long userId = BaseContext.getCurrentId();
            if (users != null && users.size() > 0 && userId != null) {
                List<Long> userIds = users.stream().map(User::getId).toList();

                // select uploader from follow where create_user = #{userId} and uploader in <userIds>
                LambdaQueryWrapper<Follow> queryWrapper1 = new LambdaQueryWrapper<Follow>()
                        .select(Follow::getUploader)
                        .eq(Follow::getCreateUser, userId)
                        .in(Follow::getUploader, userIds);
                List<Follow> follows = followService.list(queryWrapper1);
                if (follows != null && follows.size() > 0) {
                    List<Long> isFollowUploaderId = follows.stream().map(Follow::getUploader).toList();
                    for (User user : users) {
                        Long id = user.getId();
                        if (isFollowUploaderId.contains(id)) {
                            user.setIsFollow(true);
                        }
                    }
                }
            }
            return R.success(users);
        }
    }
}
